<template>
  <div class="register-container">
    <h2>用户注册</h2>
    <form @submit.prevent="handleRegister" class="register-form">
      <div class="form-group">
        <label for="username">用户名</label>
        <input
          type="text"
          id="username"
          v-model="formData.username"
          required
          placeholder="请输入用户名"
        />
      </div>

      <div class="form-group">
        <label for="email">邮箱</label>
        <input type="email" id="email" v-model="formData.email" required placeholder="请输入邮箱" />
      </div>

      <div class="form-group">
        <label for="password">密码</label>
        <input
          type="password"
          id="password"
          v-model="formData.password"
          required
          placeholder="请输入密码"
        />
      </div>

      <div class="form-group">
        <label for="confirmPassword">确认密码</label>
        <input
          type="password"
          id="confirmPassword"
          v-model="confirmPassword"
          required
          placeholder="请再次输入密码"
        />
      </div>

      <div class="error-message" v-if="error">{{ error }}</div>

      <button type="submit" class="register-btn">注册</button>

      <div class="login-link">已有账号？<router-link to="/login">立即登录</router-link></div>
    </form>
  </div>
</template>

<script>
export default {
  name: "UserRegister",
  data() {
    return {
      formData: {
        username: "",
        email: "",
        password: ""
      },
      confirmPassword: "",
      error: ""
    };
  },
  methods: {
    async handleRegister() {
      // 重置错误信息
      this.error = "";

      // 验证两次密码是否一致
      if (this.formData.password !== this.confirmPassword) {
        this.error = "两次输入的密码不一致";
        return;
      }

      try {
        await this.$store.dispatch("register", this.formData);
        // 注册成功后跳转到首页
        this.$router.push("/");
      } catch (error) {
        this.error = error.message || "注册失败，请重试";
      }
    }
  }
};
</script>

<style scoped>
.register-container {
  max-width: 400px;
  margin: 40px auto;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 24px;
}

.register-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

label {
  font-size: 14px;
  color: #666;
}

input {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

input:focus {
  outline: none;
  border-color: #4caf50;
}

.register-btn {
  background-color: #4caf50;
  color: white;
  padding: 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}

.register-btn:hover {
  background-color: #45a049;
}

.error-message {
  color: #ff4444;
  font-size: 14px;
  text-align: center;
}

.login-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.login-link a {
  color: #4caf50;
  text-decoration: none;
}

.login-link a:hover {
  text-decoration: underline;
}
</style>
